package com.cbwl.eoas.cms.web.controller.sysconfig;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbwl.eoas.cms.api.sysconfig.IDictDetailService;
import com.cbwl.eoas.cms.api.sysconfig.ISysParamService;
import com.cbwl.eoas.cms.model.sysconfig.DictDetailDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysParamDTO;
import com.cbwl.eoas.common.cache.redis.RedisKeyPrefix;
import com.cbwl.eoas.common.cache.redis.RedisManager;
import com.cbwl.eoas.common.framework.session.SessionManager;
import com.cbwl.eoas.common.framework.vo.PageRequest;
import com.cbwl.eoas.common.util.constain.DicTypeConstant;
import com.cbwl.eoas.common.web.Result;
import com.github.pagehelper.PageInfo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;



/**
 * 
 * <p>[描述信息：系统参数]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月12日 下午13:29:23
 */
@Controller
@RequestMapping("/sysparam")
public class SysParamController {
	
	
	private static Logger logger = LoggerFactory.getLogger(SysParamController.class);
	
	//业务层注解
	@Autowired
	private ISysParamService sysParamService;
	
	//业务层注解
	@Autowired
	private IDictDetailService dictDetailService;
	
	@Autowired
	private  RedisManager redisManager;
	/**
	 * <p>初始化界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initSysParam")
	public String initSysParamPage() {
		return "initSysParam";
	}


	/**
	 * <p>初始化新增修改界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initAddUpdateSysParamPage")
	public String initAddUpdateSysParamPage(HttpServletRequest request,SysParamDTO bean) {
		request.setAttribute("bean", bean);
		
		DictDetailDTO detailBean = new DictDetailDTO();
		detailBean.setDictTypeNo(DicTypeConstant.state);
		request.setAttribute("detailBean", dictDetailService.queryObjectList(detailBean));
		
		return "initAddUpdateSysParamPage";
	}
	

	/**
	 * <p>查询（分页）</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/querySysParamPage")
	@ResponseBody
	public Object querySysParamPage(SysParamDTO bean,PageRequest pageRequest) {
		
		Result result = new Result(Result.Status.OK, "查询成功", "200");
		// 分页查询用户信息
		PageInfo<?> paramPageInfo =  sysParamService.queryPageList(bean,pageRequest);
		result.setData(paramPageInfo);
		
		return result;
	}

	/**
	 * 
	 * <p>通过id查询主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/getSysParamById")
	@ResponseBody
	public Object getSysParamById(SysParamDTO bean) {
		return sysParamService.getObjectById(bean);
	}

	/**
	 * 
	 * <p>保存主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/saveSysParam")
	@ResponseBody
	public Object saveSysParam(SysParamDTO bean) throws Exception {
		bean.setCreater(SessionManager.getCurrentUser().getId());
		bean.setGmtCreate(new Date());
		logger.info("{}新建了系统参数,参数名称{}", SessionManager.getCurrentUser().getEmpName(),bean.getPrmName());
		return sysParamService.saveObject(bean);
	}

	/**
	 * 
	 * <p>更新主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/updateSysParam")
	@ResponseBody
	public Object updateSysParam(SysParamDTO bean) throws Exception {	
		
		bean.setModifier(SessionManager.getCurrentUser().getId());
		bean.setGmtModified(new Date());
		logger.info("{}更新了系统参数,参数名称{}", SessionManager.getCurrentUser().getEmpName(),bean.getPrmName());
		return sysParamService.updateObject(bean);
	}

	/**
	 * 
	 * <p>删除主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc 
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 * @throws Exception 
	 */
	@RequestMapping("/deleteSysParam")
	@ResponseBody
	public Object deleteSysParam(SysParamDTO bean) throws Exception {
		logger.info("{}删除了系统参数,参数名称{}", SessionManager.getCurrentUser().getEmpName(),bean.getPrmName());
		return sysParamService.deleteObject(bean);
	}
	
	
	@RequestMapping("/fushParamToRedis")
	@ResponseBody
	public Object fushParamToRedis(HttpServletRequest request,SysParamDTO bean){
		List<SysParamDTO> list = sysParamService.queryObjectList(bean);
		JedisPool jedisPool = redisManager.getJedisPool();
		Jedis jedis =jedisPool.getResource();
		
		Pipeline  pipeline = jedis.pipelined();
		list.forEach(dto ->{
			pipeline.set(RedisKeyPrefix.PARAM_PREFIX+dto.getPrmEnumCode(), dto.getPrmValue());
		});
		pipeline.sync();
		jedisPool.returnResourceObject(jedis);
		return 33;
	}
}
